﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using YJKDataBase;

namespace YJKArchiFormat
{
	public class StandardRoomCategory
	{
		public StandardRoomCategory(string name)
		{
			this.Category = new DbCategory();
			this.Category.Name = name;
			this.Category.Image = Resource1._null;
		}

		public StandardRoomCategory(DbCategory category)
		{
			this.Category = new DbCategory();
			this.Category = category;
		}

		public List<DbStandardRoom> GetAllRoom()
		{
			List<DbStandardRoom> list = new List<DbStandardRoom>();
			foreach (DbObject dbObject in this.Category.GetAllObject(false))
			{
				DbStandardRoom item = new DbStandardRoom(dbObject);
				list.Add(item);
			}
			return list;
		}

		public static List<DbStandardRoom> GetAllRoom(RelationDatabase database)
		{
			List<DbStandardRoom> list = new List<DbStandardRoom>();
			foreach (DbObject dbObject in database.SubjectTable.AllSubject()[0].GetAllObject(true))
			{
				DbStandardRoom item = new DbStandardRoom(dbObject);
				list.Add(item);
			}
			return list;
		}

		public static TreeView GetTree(RelationDatabase database)
		{
			TreeView treeView = new TreeView();
			StandardRoomCategory.FillTree(treeView, new List<RelationDatabase>
			{
				database
			});
			return treeView;
		}

		public static void FillTree(TreeView tree, List<RelationDatabase> dbs)
		{
			try
			{
				tree.Nodes.Clear();
				for (int i = 0; i < dbs.Count<RelationDatabase>(); i++)
				{
					StandardRoomCategory.DataBaseFillTree(dbs[i], tree);
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private static void DataBaseFillTree(RelationDatabase db, TreeView tree)
		{
			try
			{
				List<DbCategory> all = db.CategoryTable.GetAll();
				DbSubject dbSubject = db.SubjectTable.AllSubject()[0];
				TreeNode treeNode = new TreeNode(dbSubject.ToString());
				treeNode.Name = dbSubject.Name;
				treeNode.Tag = dbSubject;
				StandardRoomCategory.SetChildNode(treeNode, all);
				tree.Nodes.Add(treeNode);
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private static void SetChildNode(TreeNode tn, List<DbCategory> cl)
		{
			foreach (DbCategory dbCategory in cl)
			{
				if (tn.Tag is DbCategory)
				{
					if (dbCategory.ParentId == ((DbCategory)tn.Tag).Guid)
					{
						TreeNode treeNode = new TreeNode(dbCategory.ToString());
						treeNode.Name = dbCategory.Name;
						treeNode.Tag = dbCategory;
						treeNode.ImageIndex = 1;
						if (dbCategory.Limit)
						{
							treeNode.ForeColor = System.Drawing.Color.Red;
						}
						foreach (DbObject dbObject in dbCategory.GetAllObject(false))
						{
							DbStandardRoom dbStandardRoom = new DbStandardRoom(dbObject);
							TreeNode treeNode2 = new TreeNode(dbStandardRoom.DbObject.Name);
							treeNode2.Tag = dbStandardRoom;
							treeNode.Nodes.Add(treeNode2);
						}
						StandardRoomCategory.SetChildNode(treeNode, cl);
						tn.Nodes.Add(treeNode);
					}
				}
				else if (tn.Tag is DbSubject)
				{
					DbSubject dbSubject = (DbSubject)tn.Tag;
					if (dbCategory.ParentId == dbSubject.Guid)
					{
						TreeNode treeNode3 = new TreeNode(dbCategory.ToString());
						treeNode3.Name = dbCategory.Name;
						treeNode3.Tag = dbCategory;
						treeNode3.ImageIndex = 1;
						if (dbCategory.Limit)
						{
							treeNode3.ForeColor = System.Drawing.Color.Red;
						}
						foreach (DbObject dbObject2 in dbCategory.GetAllObject(false))
						{
							DbStandardRoom dbStandardRoom2 = new DbStandardRoom(dbObject2);
							TreeNode treeNode4 = new TreeNode(dbStandardRoom2.DbObject.Name);
							treeNode4.Tag = dbStandardRoom2;
							treeNode3.Nodes.Add(treeNode4);
						}
						StandardRoomCategory.SetChildNode(treeNode3, cl);
						tn.Nodes.Add(treeNode3);
					}
				}
			}
		}

		public DbCategory Category;
	}
}
